package test4_12_2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.UUID;

public class ThreadServer implements Runnable {
private Socket socket;

    public ThreadServer(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {

        BufferedReader br = null;
        BufferedWriter bw = null;
        BufferedWriter bos = null;
        try {
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("欢迎光临！请上传文件(文本文件)！".getBytes());
            outputStream.write("\r\n".getBytes());

            //创建网络字节输入流，读取客户端上传的文件：
            InputStream inputStream = socket.getInputStream();
            //转换为字符流：
            InputStreamReader isr = new InputStreamReader(inputStream);
            br = new BufferedReader(isr);

            //创建本地字符输出流，将改好的文本写入硬盘：
            bw = new BufferedWriter(new FileWriter(
                    "D:\\IdeaProjects\\Job-code\\workTest\\src\\test4_12_2\\"+UUID.randomUUID()+".txt"));

            String len;

            while ((len = br.readLine()) != null) {
                if (len.contains("操")){
                    len = len.replace("操","**");
                }
                bw.write(len);
                bw.newLine();
                bw.flush();
            }


            //给客户端回写信息：  上传成功
            outputStream.write("上传成功".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
